import { defineStore } from 'pinia'

const useSkuCuntStore = defineStore('sku', {
    state: () => {
        return {
            good: new Map(),
            currentColor: {},
            /** 规格选择页面 当前已经选择的规格数组 */
            selectSkus: [] as Array<any>
        }
    },
    actions: {
        updateGood(data: any) {
            this.good = data
        },
        updateCurrentColor(data: any) {
            this.currentColor = data
        }
    },
    getters: {
        colorBtns() {
            console.log('执行 getters');
            let btns = new Map()
            this.good.forEach(entry => {
                let temp: Array<any> = []
                let total = 0
                let skus = entry.goodsReleaseSkuVo
                skus.forEach((entries: any)=>{
                    let allSizeStock = entries.some((m: any) => m.stock > 0)
                    entries.map((m: any)=> total += m.num)
                    temp.push({
                        collar: entry.name,
                        picId: entries[0].picId, // || colors[0].picIds && colors[0].picIds[0],
                        colorName: entries[0].colorName,
                        badge: entries.map((m: any) => m.num).reduce((pre: number,cur:number)=>{return pre + cur}),
                        skus: entries,
                        saleout: !allSizeStock,
                        sum: entries.map((m: any) => m.num * m.userPrice).reduce((pre: number,cur:number)=>{return pre + cur})
                    })
                })
                entry.badge = total
                btns.set(entry.name, temp)
            })
            return btns
        },
        collars(): Array<any> {
            let temp: Array<any> = []
            this.good.forEach(item => {
                temp.push({
                    collarImage: item.collarImage,
                    modelNo:item.modelNo,
                    spuId:item.spuId,
                    badge:item.badge,
                    name:item.name
                })
            })
            return temp
        },
        /** 已选的规格 */
        selectList(): Array<any> {
            return []
        }
    }
})

export default useSkuCuntStore
